package com.angel.arquitectura.util.hash;

import com.angel.arquitectura.exception.NonBusinessException;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Varios
 * @since 30/April/2009
 */
public final class HashUtils {

    private static final String ALGORITHM = "SHA-256";
    private static final char[] DECIMAL_HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    public static String hash(String text) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(text.getBytes());
            byte[] digest = md.digest();

            StringBuffer sb = new StringBuffer(digest.length * 2);
            for (int i = 0; i < digest.length; ++i) {
                byte b = digest[i];
                int high = (b & 0xF0) >> 4;
                int low = b & 0xF;
                sb.append(DECIMAL_HEX[high]);
                sb.append(DECIMAL_HEX[low]);
            }
            return sb.toString();
        }
        catch (NoSuchAlgorithmException e) {
            throw new NonBusinessException("Error hashing string", e);
        }
    }
}
